/**
 *  Copyright 2012 oliver.keusemann@googlemail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package de.xelerity.geolessi.srv.impl.routing.bahn;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import de.xelerity.geolessi.preferences.Preferences;
import de.xelerity.geolessi.srv.ServiceProvider;
import de.xelerity.geolessi.srv.impl.poi.bahn.HalteStelleNextAbfahrtGetterImpl;
import de.xelerity.geolessi.srv.mixed.URLGenerator;
import de.xelerity.geolessi.srv.mixed.WSCallerI;
import de.xelerity.geolessi.types.poi.HalteStelle;
import de.xelerity.geolessi.types.poi.HaltestellenIDProvider;
import de.xelerity.geolessi.types.routing.Route;
import de.xelerity.helper.DateFunktionen;
import de.xelerity.helper.StringFunktionen;

public class RouteGetter {

	private Log log = LogFactory.getLog(RouteGetter.class);

	
	public List<Route> getRoute(HalteStelle Start, HalteStelle End, long StartTime, long CurrentTime) throws Exception
	{
		
		List<String> iDs1 = Start.getIDs(HaltestellenIDProvider.BAHN);
		if(iDs1==null||iDs1.size()==0)
		{
			throw new Exception("Kein Startpunkt definiert. Haltestellenprovider: Bahn");
		}
		List<String> iDs2 = End.getIDs(HaltestellenIDProvider.BAHN);
		if(iDs2==null||iDs2.size()==0)
		{
			throw new Exception("Kein Endpunkt definiert. Haltestellenprovider: Bahn");
		}
		
		String URL = setUpUrl(iDs1.get(0), iDs2.get(0),StartTime);

		System.out.println(URL);
		
		
		WSCallerI wsCaller2 = ServiceProvider.getWSCaller();

		if (Preferences.useProxy) {
			wsCaller2.addProperty("PROXYHOST", Preferences.Proxy);
			wsCaller2.addProperty("PROXYPORT", Preferences.ProxyPort + "");
		}

		byte[] callResB = wsCaller2.call(URL);

		System.out.println(new String(callResB,Preferences.ENC_ISO));
		
		parseResult(new String(callResB,Preferences.ENC_ISO), Start, End, CurrentTime);
		
		return null;
	}
	
	private void parseResult( String S, HalteStelle StartHS, HalteStelle EndHS, long currentTime ) {
		if (StringFunktionen.isEmptyTrimed(S)) {
			return ;
		}
		ResultParser resultParser = new ResultParser(S, currentTime);

		resultParser.getStartEndAdressBez(StartHS, EndHS );
		// HTML-Parser http://jsoup.org/
		
		System.out.println("von:" + StartHS.getAddressbez());
		System.out.println("nach:" + EndHS.getAddressbez());
	
		Route lRoute =  new Route();
		lRoute.setVon(StartHS);
		lRoute.setBis(EndHS);
		
		resultParser.getRoute(StartHS, EndHS);
	}
	
	
	
	
	
	
	
	private String setUpUrl(String pHSIDStart, String pHSIDEnd , long pCurrentTime) throws Exception {

		URLGenerator urlGenerator = new URLGenerator("mobile.bahn.de", "bin/mobil/query2.exe/dox");

		urlGenerator.addParameter("rt","1");
		urlGenerator.addParameter("use_realtime_filter","1");
		urlGenerator.addParameter("sotRequest","1");
		urlGenerator.addParameter("REQ0JourneyStopsS0A","1");
		urlGenerator.addParameter("REQ0JourneyStopsS0G",pHSIDStart);
		urlGenerator.addParameter("REQ0JourneyStopsS0ID","");
		urlGenerator.addParameter("REQ0JourneyStopsZ0A","1");
		urlGenerator.addParameter("REQ0JourneyStopsZ0G",pHSIDEnd);
		urlGenerator.addParameter("REQ0JourneyStopsZ0ID","");
		urlGenerator.addParameter("REQ0JourneyDate",DateFunktionen.getTimeAsFormat(pCurrentTime, "dd.MM.yyyy"));
		urlGenerator.addParameter("REQ0JourneyTime",DateFunktionen.getTimeAsFormat(pCurrentTime, "HH:mm"));
		urlGenerator.addParameter("existOptimizePrice","1");
		urlGenerator.addParameter("REQ0HafasOptimize1","0:1");
		urlGenerator.addParameter("existProductNahverkehr","yes");
		urlGenerator.addParameter("REQ0JourneyProduct_prod_list","4:0001111111000000");
		urlGenerator.addParameter("immediateAvail","ON");
		urlGenerator.addParameter("start","Suchen");

		return urlGenerator.getURL();
	}
	
	public static void main(String[] args) {
		
		
		HalteStelle Start = new HalteStelle();
		Start.addHaltestellenID(HaltestellenIDProvider.BAHN, "562247");

		HalteStelle End = new HalteStelle();
		End.addHaltestellenID(HaltestellenIDProvider.BAHN, "561591");
		
		long StartTime = new java.util.Date().getTime();
		long CurrentTime = StartTime;
		List<Route> route =null;
		try {
			route = new RouteGetter().getRoute(Start, End, StartTime, CurrentTime);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
